﻿using System;
using System.IO;
using System.Collections.Generic;

namespace DatabaseStuff.Basic
{
    public class FileDestination: TextDestination
    {
        private string folderpath;

        public FileDestination()
            : base()
        {
            folderpath = String.Empty;
        }

        public FileDestination(string _folderpath)
            : base()
        {
            InitializeFolderpath(_folderpath);
        }

        internal FileDestination(TextStorageFormatSettings settings)
            : base(settings)
        {
            folderpath = String.Empty;
        }

        internal FileDestination(TextStorageFormatSettings settings, string _folderpath)
            : base(settings)
        {
            InitializeFolderpath(_folderpath);
        }

        private void InitializeFolderpath(string _folderpath)
        {
            folderpath = _folderpath;
            if (!folderpath.EndsWith("\\"))
                folderpath += "\\";
        }

        public string FolderPath
        {
            get { return folderpath; }
            set { folderpath = value; }
        }

        protected override TextWriter GetTableNamesWriter(IDatabase db)
        {
            return new StreamWriter(File.Open(folderpath + "tables.dat", FileMode.Create));
        }

        protected override TextWriter GetRecordsWriter(IDBTable table)
        {
            return new StreamWriter(File.Open(folderpath + table.Name + ".table", FileMode.Create));
        }

        protected override TextWriter GetSchemaWriter(IDBTable table)
        {
            return new StreamWriter(File.Open(folderpath + table.Name + ".schema", FileMode.Create));
        }
    }
}
